package guiPackage;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class OpvulMethoden implements ActionListener {
	private static String[] intList = new String[] { "random int", "oplopend int", "null" };

	private static String[] doubleList = new String[] { "random double", "oplopend double", "null" };

	private static String[] stringList = new String[] { "voornaam", "achternaam", "plaatsnaam",
			"straatnaam", "huisnummer", "adres", "postcode", "email", "gebruikersnaam",
			"wachtwoord", "telnr", "bestand", "nummer", "null" };

	private static String[] dateTimeList = new String[] { "random datetime", "null" };
	private static String[] dateList = new String[] { "random date", "null" };

	private JComboBox dropDownList;
	private JPanel methodenPanel, argPanel;
	private JTextField[] args;
	private String kolomType, kolomNaam, selectedNaam, tabelNaam;
	private JButton openBestandButton;
	private JFileChooser fileChooser;

	public OpvulMethoden(JPanel methodenPanel) {
		this.methodenPanel = methodenPanel;
		argPanel = new JPanel();
	}

	public JPanel getMethodenPanel(String kolomType) {
		this.kolomType = kolomType;
		methodenPanel = new JPanel();
		methodenPanel.add(getDropDownList());
		dropDownList.setSelectedIndex(0);
		return methodenPanel;
	}

	public JComboBox getDropDownList() {
		if (kolomType.equals("INT")) {
			dropDownList = new JComboBox(intList);
		}
		if (kolomType.equals("DECIMAL")) {
			dropDownList = new JComboBox(doubleList);
		}
		if (kolomType.equals("VARCHAR")) {
			dropDownList = new JComboBox(stringList);
		}
		if (kolomType.equals("DATETIME")) {
			dropDownList = new JComboBox(dateTimeList);
		}
		if (kolomType.equals("DATE")) {
			dropDownList = new JComboBox(dateList);
		}
		dropDownList.addActionListener(this);
		return dropDownList;
	}

	public void createArgs(int aantal) {
		args = new JTextField[aantal];
		for (int i = 0; i < aantal; i++) {
			args[i] = new JTextField(5);
		}
	}

	public void maakArgumentenPanel(String[] labelTekst) {
		argPanel.removeAll();
		if (labelTekst != null) {
			args = new JTextField[labelTekst.length];
			for (int i = 0; i < labelTekst.length; i++) {
				args[i] = new JTextField(5);
				argPanel.add(new JLabel(labelTekst[i]));
				argPanel.add(args[i]);
			}
			methodenPanel.add(argPanel);
		} else {
			args = null;
		}
		methodenPanel.revalidate();
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == dropDownList) {
			JComboBox cb = (JComboBox) e.getSource();
			selectedNaam = (String) cb.getSelectedItem();
			if (selectedNaam.equals("random int")) {
				maakArgumentenPanel(new String[] { "min", "max" });
			} else if (selectedNaam.equals("oplopend int")) {
				maakArgumentenPanel(new String[] { "min" });
			} else if (selectedNaam.equals("random double")) {
				maakArgumentenPanel(new String[] { "min", "max" });
			} else if (selectedNaam.equals("oplopend double")) {
				maakArgumentenPanel(new String[] { "start", "inc" });
			} else if (selectedNaam.equals("bestand")) {
				maakArgumentenPanel(new String[] { "file" });
				fileChooser = new JFileChooser();
				fileChooser.addActionListener(this);
				openBestandButton = new JButton("open");
				openBestandButton.addActionListener(this);
				methodenPanel.add(openBestandButton);
			} else if (selectedNaam.equals("random datetime")) {
				maakArgumentenPanel(new String[] { "begindatetime", "einddatetime", "min uur",
						"max uur" });
			} else if (selectedNaam.equals("random date")) {
				maakArgumentenPanel(new String[] { "begindatum", "einddatum" });
			} else if (selectedNaam.equals("nummer")) {
				maakArgumentenPanel(new String[] { "aantal cijfers", "min", "max" });
			} else {
				maakArgumentenPanel(null);
			}
		}

		if (e.getSource() == openBestandButton) {
			File bestand;
			String path;
			int returnVal = fileChooser.showOpenDialog(methodenPanel);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				bestand = fileChooser.getSelectedFile();

				// kijk waar het bestand staat (later nodig bij wegschrijven)
				Scanner sc = new Scanner(bestand.getPath());
				sc.useDelimiter(bestand.getName());
				path = sc.next();
				sc.close();

				// Zet het pad naar het bestand
				args[0].setText(path + bestand.getName());
			}
		}

	}

	public JTextField[] getArgs() {
		return args;
	}

	public String getKolomType() {
		return kolomType;
	}

	public String getSelectedNaam() {
		return selectedNaam;
	}

	public String getTabelNaam() {
		return tabelNaam;
	}

	public void setTabelNaam(String tabelNaam) {
		this.tabelNaam = tabelNaam;
	}

	public String getKolomNaam() {
		return kolomNaam;
	}

	public void setKolomNaam(String kolomNaam) {
		this.kolomNaam = kolomNaam;
	}
}
